/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import model.*;
import entities.*;
import java.util.*;

/**
 *
 * @author xuanthuy
 */
@Named(value = "domainManagedBean")
@SessionScoped
public class DomainManagedBean implements Serializable {

    @EJB
    private DomainFacade domainFacade;
    private Domain domain = new Domain();

    private String message = "";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public Domain getDomain() {
        return domain;
    }

    public void setDomain(Domain domain) {
        this.domain = domain;
    }

    public DomainManagedBean() {
    }

    // find all
    public List<Domain> sudungtruyvanAll() {
        return this.domainFacade.truyvanAll();
    }

    //them
    public String add() {
        return "domainAdAdd?faces-redirect=true";
    }
    
    public String addDomain() {
        Domain temp = this.domainFacade.checkExists(domain.getName());
        if (temp == null) {
            this.domain.setStatus(Boolean.TRUE);
            this.domainFacade.create(this.domain);
            return "domainAd?faces-redirect=true";
        } else {
            this.message = domain.getName()+ " already exists! Please choose Domain different!";
            return "";
        }
    }
    
    public void clear() {
        this.domain.setName("");
        this.domain.setDescriptions("");
    }
    
    // edit
    public String edit(Domain domain) {
        this.domain = domain;
        return "domainAdEdit";
    }
    
    public String xulycapnhat() {
        this.domainFacade.edit(this.domain);
        return "domainAd?faces-redirect=true";
    }
    
    // xoa An
    public void xoaAn(Domain domain) {
        this.domain = domain;
        this.domain.setStatus(Boolean.FALSE);
        this.domainFacade.edit(this.domain);
    }

    //phuc hoi
    public String recoverDatadelete() {
        return "domainAdRecover";
    }
    
    public List<Domain> sudungtruyvan1() {
        return this.domainFacade.truyvan1();
    }
    
    public String recoverData(Domain domain) {
        this.domain = domain;
        this.domain.setStatus(Boolean.TRUE);
        this.domainFacade.edit(this.domain);
        return "domainAd?faces-redirect=true";
    }
    
    // xoa luon
    public void xoa(Domain domain) {
        this.domainFacade.remove(domain);
    }
}
